import gtk,glib
import hildondesktop
import dbus, time, math
import cairo

class HelloHomePlugin(hildondesktop.HomePluginItem):
	def __init__(self):
		hildondesktop.HomePluginItem.__init__(self)
		self.box1 = gtk.VBox(False, 0)

		self.current = gtk.Label('no readings')	
		self.current.show_all()	

		self.add(self.current)
		self.timeout_handler = glib.timeout_add_seconds(30, self.updateReading, self)
		self.bus = dbus.SystemBus()             
		self.cur = self.bus.get_object('org.freedesktop.Hal',
			'/org/freedesktop/Hal/devices/bme')
		#self.logfile = open('\home\user\MyDocs\sppi-current.dat','a')
		###	
		self.updateReading(self)


	def updateReading(self,w):
		cur = self.cur.GetProperty('battery.reporting.current')
		cur2 = self.cur.GetProperty('battery.charge_level.percentage')
		str_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
		content = str_time +'\t'+ str(cur)+'\t'+str(cur2)+'\n'
		self.log(content)
		self.current.set_text(str(cur)+' mAh ('+str(cur2)+'%)')
		return True

	def log(self, content):
		f = open('/home/user/MyDocs/sppi-current.dat','a')
		f.write(content)
		f.close()
		
	def do_expose_event(s, e):
		a = e.area
		cr = s.window.cairo_create()
		cr.arc(a.width/2,a.height/2,a.width/2,0,2*math.pi)
		cr.set_source_rgba(0.0,0.0,0.0,0.625)
		cr.fill()
		hildondesktop.HomePluginItem.do_expose_event(s,e)

	def do_realize(s):
		scn = s.get_screen()
		cm = scn.get_rgba_colormap()
		s.set_colormap(scn.get_rgba_colormap())
		s.set_app_paintable(True)
		hildondesktop.HomePluginItem.do_realize(s)
	
	def do_destroy(self):
		import sys
		sys.exit(1)

hd_plugin_type = HelloHomePlugin

# The code below is just for testing purposes.
# It allows to run the widget as a standalone process.
if __name__ == "__main__":
    import gobject
    gobject.type_register(hd_plugin_type)
    obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")
    obj.show_all()
    gtk.main()

